package qq.listener;

import catcode.CatCodeUtil;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.annotation.Priority;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.constant.PriorityConstant;
import org.apache.ibatis.session.SqlSession;
import qq.bean.User;
import qq.mapper.UserMapper;
import qq.service.*;
import qq.tool.*;
import qq.utils.RecentMessageUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Beans
public class Listener {
    @OnGroup
    @Priority(PriorityConstant.FIRST)
//    @Filter(groups = "482929073")
    public void maren(GroupMsg groupMsg, MsgSender sender) throws IOException {
        String message = groupMsg.getMsg();
        if(message.equals("骂他") && RecentMessageUtil.maRen){
            CatCodeUtil catCodeUtil = CatCodeUtil.INSTANCE;
            String at = catCodeUtil.toCat("at","code="+ RecentMessageUtil.recentId);
            sender.SENDER.sendGroupMsg(groupMsg,at+"傻逼");
        }
    }

    /**
     * 监听消息开头为 "mua" 或者 "hi" 的消息并复读。
     */
    @OnGroup
//    @Filter(groups = "482929073")
    public void groupMsg(GroupMsg groupMsg, MsgSender sender) throws IOException {
        System.out.println(groupMsg);
        String message = groupMsg.getMsg();
        long id = Long.parseLong(groupMsg.toString().split("=")[5].split(",")[0]);
        RecentMessageUtil.recentId = id;
        System.out.println(groupMsg.getMsgContent().getMsg()+"================");
        if (message.equals("mua")){
            sender.SENDER.sendGroupMsg(groupMsg, "傻逼");
            return;
        }
        if (message.equals("夸我")){
            String msg1 = CaiHongPiService.INSTANCE.caihongpi("");
            sender.SENDER.sendGroupMsg(groupMsg,msg1);
            return;
        }

        if (message.equals("图片")){
//            User user = SignInService.viewPoint(id);
            SqlSession sqlSession = MybatisTool.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            if (userMapper.getUser(id).getPoints()<2){
                sender.SENDER.sendGroupMsg(groupMsg,"积分不足");
                return;
            }
            userMapper.declinePoint(id,3);
            sqlSession.commit();

            int random = (int)(Math.random()*2);
            String tupian=null;
            if (random==0){
                tupian = TuPianService.getTuPian("https://www.tupianzj.com/meinv/yishu/");
            }else if (random==1){
                tupian = TuPianService.getTuPian("https://www.tupianzj.com/meinv/xinggan/");
            }
            sender.SENDER.sendPrivateMsg(groupMsg,"[CAT:image,url="+tupian+"]");
            return;
            }
//        if (message.equals("图片集")){
//            SqlSession sqlSession = MybatisTool.getSqlSession();
//            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//            if (userMapper.getUser(id).getPoints()<2){
//                sender.SENDER.sendGroupMsg(groupMsg,"积分不足");
//                return;
//            }
//            userMapper.declinePoint(id,3);
//            sqlSession.commit();
//            ArrayList<String> urls = TuPianJiService.getDMUrls();
//            for (String url:urls) {
//                sender.SENDER.sendPrivateMsg(groupMsg,"[CAT:image,url="+url+"]");
//            }
//            return;
//        }

        if (message.equals("艾特")){
            CatCodeUtil catCodeUtil = CatCodeUtil.INSTANCE;
            String at = catCodeUtil.toCat("at","code="+id);
            sender.SENDER.sendGroupMsg(groupMsg,at);
        }

        if (message.equals("个人信息")){
            User user = UserSevice.getUser(id);
            sender.SENDER.sendGroupMsg(groupMsg, user.toString());
            return;
        }
        if(message.equals("抽奖")){
            String result = RPGService.choujiang(id);
            sender.SENDER.sendGroupMsg(groupMsg, result);
            return;
        }
        if(message.equals("积分榜")){
            List<User> rank = UserSevice.getRank();
            StringBuilder sb = new StringBuilder();
            for (int i=1;i<= rank.size();i++){
                sb.append("排行"+i+":"+rank.get(i-1).getId()+", 积分："+rank.get(i-1).getPoints()+"\n");
            }
            sender.SENDER.sendGroupMsg(groupMsg,sb.toString());
        }

    }

    @OnGroup
    @Filter(anyAt = true)
    public void atMsg(GroupMsg groupMsg,MsgSender sender) throws IOException {
        if (OwnerTool.dajieStatus==false && groupMsg.getMsg().contains("打劫")){
            sender.SENDER.sendGroupMsg(groupMsg,"打劫功能已关闭");
            return ;
        }

        String message = groupMsg.getMsg().split(" ")[1];
        if (message.equals("打劫")){
            long id1 = Long.parseLong(groupMsg.toString().split("=")[5].split(",")[0]);
            long id2 = Long.parseLong(groupMsg.getMsg().split("=")[1].split("]")[0]);
            String result = RPGService.dajie(id1,id2);
            sender.SENDER.sendGroupMsg(groupMsg,result);
            return;
        }
        if (message.contains("赠送")){
            long id1 = Long.parseLong(groupMsg.toString().split("=")[5].split(",")[0]);
            long id2 = Long.parseLong(groupMsg.getMsg().split("=")[1].split("]")[0]);
            SqlSession sqlSession = MybatisTool.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            synchronized (this){
                int point = Integer.parseInt(message.split("赠送")[1]);
                if (userMapper.getUser(id1).getPoints()>=point && point>0){
                    UserSevice.declinePoint(id1,point);
                    UserSevice.addPoint(id2,point);
                    sender.SENDER.sendGroupMsg(groupMsg,"你赠送了他"+point+"分");
                    return;
                }else {
                    if (point<0){
                        sender.SENDER.sendGroupMsg(groupMsg,"请输入大于0的数");
                        return ;
                    }
                    sender.SENDER.sendGroupMsg(groupMsg,"积分不足");
                    return;
                }
            }


        }
        return ;

    }

    @OnGroup
    @Filter(atBot = true)
    public void groupMsg2(GroupMsg groupMsg,MsgSender sender) throws IOException {
        String message = groupMsg.getMsg().split(" ")[1];
        long id = Long.parseLong(groupMsg.toString().split("=")[5].split(",")[0]);
        if (message.equals("签到")){
            String signInMessage= SignInService.signIn(id);
            sender.SENDER.sendGroupMsg(groupMsg,signInMessage);
            return;
        }
        if (message.equals("菜单") ||message.equals("功能")){
            sender.SENDER.sendGroupMsg(groupMsg,
                    "2、mua\n" +
                    "3、@机器人 签到，\n" +
                    "4、@机器人 查看积分，\n" +
                    "5、夸我\n" +
                    "6、@机器人 城市天气，\n" +
                    "7、@机器人 成语接龙,\n" +
                    "8、给我康康\n" +
                    "9、打劫\n" +
                    "10、抽奖");
            return;
        }
        if (message.equals("查看积分")){
            User user = SignInService.viewPoint(id);
            if (user != null)
                sender.SENDER.sendGroupMsg(groupMsg,"当前积分为："+user.getPoints());
            else
                sender.SENDER.sendGroupMsg(groupMsg,"你还未签到过没有积分");
            return;
        }

        if (message.contains("天气")){
            String city = message.split("天气")[0];
            String weather = TianQiService.INSTANCE.getWeather(city);
            sender.SENDER.sendGroupMsg(groupMsg,weather);
            return;
        }

        if (message.length()==4){
            String jielong = ChengyuService.INSTANCE.jieLong(message);
            sender.SENDER.sendGroupMsg(groupMsg,jielong);
            return;
        }
    }
}
